﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;


namespace map.manage
{
    public partial class area3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Literal_map.Text = RuleCode.Util.getmapurl();
                //string provice = HttpContext.Current.Request.Cookies["f980"]["area"];
                //if (string.IsNullOrEmpty(provice))
                //{
                //    provice = "北京";
                //}
                bind2();


            }
            CountyDataBind();
        }
        private void bind2()
        {

            if (!IsPostBack)
            {
                string provice = zfunction.Province;

                RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + provice + "' ");
                DropDownList2.DataSource = x.MyTable.DefaultView;
                DropDownList2.DataTextField = "县编码";
                DropDownList2.DataValueField = "县编码";
                DropDownList2.DataBind();

                ////CountyDataSoruce.SelectCommand = "SELECT [县编码] FROM [县] where [省编码]='" + provice + "'";
                ////DropDownList2.DataBind();
//DropDownList2.Items.Add("昌平");
                string ScriptTag = "";

                //ScriptTag =
                //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                ScriptTag =
                    "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                    + "" + "39.917043746896674,'" + "2222" + "');</script>";

                ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            }
        }

        /// <summary>
        /// 区域表格
        /// </summary>


        //protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    CountyDataSoruce.SelectCommand = "SELECT [县编码] FROM [县] where [省编码]='" + DropDownList1.SelectedValue + "'";
        //    //CountyDataSoruce
        //    //CountyDataSoruce.Select();
        //    CountyDataSoruce.Select(null);
        //    DropDownList2.DataBind();
        //    DropDownList2.Visible = true;
        //}
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList2.Text != "")
            {
                区 uc = 区.Find(" 1=2 ");
                uc.New();
                //uc.区编码.Value = DropDownList2.Text;
                uc.县编码.Value = DropDownList2.Text;
                uc.区编码.Value = newDistrict.Text;
                uc.taobaolong.Value = zfunction.str2double(mapLng.Text);
                uc.taobaolat.Value = zfunction.str2double(MapLat.Text);
                uc.Insert();
                CountyDataBind();
            }
            else
            {
                MessageBox.Show(this.UpdatePanel1, "操作不成功.");
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            区 x = 区.Find(" recid = '" + Label_recid.Text + "'", "RECID");
            x.区编码.Value = newDistrict.Text;
            x.taobaolong.Value = zfunction.str2double(mapLng.Text);
            x.taobaolat.Value = zfunction.str2double(MapLat.Text);
            x.Modify();
            CountyDataBind();
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            区 uc = 区.Find(" recid = '" + id + "'", "RECID");

            //TextBox1.Text = uc.cbusercataid.ToString();
            //TextBox2.Text = uc.cbcataid.ToString();
            //TextBox3.Text = uc.jcdesc.ToString();
            Label_recid.Text = uc.RECID.ToString();

            newDistrict.Text = uc.区编码.ToString();
            mapLng.Text = uc.taobaolong.ToString();
            MapLat.Text = uc.taobaolat.ToString();

            string ScriptTag = "";
            if (uc.taobaolong.Value == 0 || uc.taobaolat.Value == 0)
            {
                ScriptTag =
                    "markpos2(" + "" + "116.39679908752441,"
                    + "39.917043746896674" + ",'" + uc.区编码.ToString() + "')";
            }
            else
            {
                ScriptTag =
                    "markpos2(" + uc.taobaolong.ToString() + ","
                    + uc.taobaolat.ToString() + ",'" + uc.区编码.ToString() + "')";
            }
            ScriptManager.RegisterStartupScript(
                this.Page, this.Page.GetType(), "bodyevent3333", ScriptTag, true);

        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            CountyDataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            区 uc = 区.Find(" recid = '" + id + "'", "RECID");
            uc.Delete();

            CountyDataBind();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            CountyDataBind();
            string provice = zfunction.Province;
            string county = DropDownList2.SelectedValue;
            string query = provice + "," + county;
            string ScriptTag = "showAddress('" + query + "');";
            ScriptManager.RegisterStartupScript(
            this.Page, this.Page.GetType(), "bodyevent333", ScriptTag, true);

            //GridView1.DataBind();

        }

        
        private void CountyDataBind()
        {
            区 uc = 区.Find(" 县编码 = '" + DropDownList2.SelectedValue + "'", "RECID");
            GridView1.DataSource = uc.MyTable;
            GridView1.DataBind();

          
        }

        //protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        //    string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        //    区 x = 区.Find(" recid = '" + id + "'", "RECID");
        //    x.区编码.Value = e.NewValues[2].ToString();
        //    x.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
        //    x.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
        //    x.Modify();
        //    bind3();
        //}

    }
}
